/**
 * Created by wangdi on 20/11/16.
 */
'use strict';
import React, {Component} from 'react';

//react-native自带安卓返回键监听模块
import {BackAndroid} from 'react-native';

//监听android的返回键，主要是做给二级界面或者层次更深的界面用的
export default class BackPageComponent extends Component {
    constructor(props){
        super(props);
    }

    //注册监听器
    componentDidMount() {
        BackAndroid.addEventListener('hardwareBackPress', this._handleBack.bind(this));
    }

    //移除监听器
    componentWillUnmount() {
        BackAndroid.removeEventListener('hardwareBackPress', this._handleBack.bind(this));
    }

    //返回上级界面，继承此控件的控件可以调用
    _handleBack(){
        const navigator = this.props.navigator;
        if (navigator && navigator.getCurrentRoutes().length > 1){
            navigator.pop();
            return true;
        }
        return false;
    }
}